package com.yupi.springbootinit.controller;

import com.yupi.springbootinit.websocket.WebSocketServer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;

@RestController
@Slf4j
public class WebSocketController {

    @Resource
    WebSocketServer webSocketServer;

    @GetMapping("/test/send/ws/{userId}")
    public String send2WS(@PathVariable String userId){
        try {
            webSocketServer.sendMessage("this is a message from server!", new HashSet<>(Collections.singletonList(String.valueOf(userId))));
            return "connect webSocket succeed";
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}